<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <input type="text" v-model="n1" />
    <input type="text" v-model="n2" />
    <input type="button" value="加" @click="f('+')" />
    <input type="button" value="减" @click="f('-')" />
    <input type="button" value="乘" @click="f('*')" />
    <input type="button" value="除" @click="f('/')" />
    <h3>结果：{{result}}</h3>
</div>
</body>
<script src="js/vue.min.js"></script>
<script>
    let v = new Vue({
        el:"body>div",
        data:{
            n1:"", n2:"",result:""
        },
        methods:{
            f(type){
                if(isNaN(v.n1) || isNaN(v.n2)){
                    v.result = "输入错误";
                    return;
                }
                //eval()方法可以将字符串以js代码的形式运行,将运行结果返回
                v.result = eval(v.n1 + type + v.n2);
                /*v.n1 = parseFloat(v.n1);
                v.n2 = parseFloat(v.n2);
                switch(type){
                    case 1:v.result=v.n1+v.n2;break;
                    case 2:v.result=v.n1-v.n2;break;
                    case 3:v.result=v.n1*v.n2;break;
                    case 4:v.result=v.n1/v.n2;break;
                }*/
            }
        }
    });

</script>
</html>